<?php
include "../include/global.php";
include "../include/admin_check.php";
$xtpl = new XTemplate ("daytripWrite.htm");
include "setting.php";
include "../include/function.php";

$old_daytrip_image=$_POST['old_daytrip_image'];

$country_id=intval($_POST['country_id']);
$city_id=intval($_POST['city_id']);
$daytrip_name=formatDataForMySql($_POST['daytrip_name']);
$daytrip_price=formatDataForMySql($_POST['daytrip_price']);
$daytrip_brief=formatDataForMySql($_POST['daytrip_brief']);
$daytrip_content=formatData($_POST['daytrip_content']);
$travelstyle_id=$_POST['travelstyle_id'];
$daytrip_position=intval($_POST['daytrip_position']);
$daytrip_isdestination=intval($_POST['daytrip_isdestination']);
$daytrip_isactive=intval($_POST['daytrip_isactive']);
$daytrip_metadescription=formatDataForMySql($_POST['daytrip_metadescription']);
$daytrip_metakeyword=formatDataForMySql($_POST['daytrip_metakeyword']);

$ID=intval(_POST('ID'));
$type=$_POST['type'];

//
$date = date("Y-m-d");
$sub_mit = $_POST['sub_mit'];
if($sub_mit=="yes"){
	if($daytrip_name==""){
		$error_daytrip_name ="Bạn phải nhập Tên <br />";
	}
	//UPLOAD NEW FILE
	if(!$DELETE_ORIGIN_IMAGE)
	{
		if($err=="")
		{
			if($_FILES['daytrip_image'])
			{
				$daytrip_image=uploadfile($path, $_FILES['daytrip_image'],0,$MAX_FILE_SIZE);
			}
		}
		if($daytrip_image=='-1')
		{
			$error_daytrip_image.="Lỗi, không thể upload";
			$err="yes";
		}
		else if($daytrip_image=='0')
		{
			$error_daytrip_image.="File không đúng kiểu";
			$err="yes";
		}
		else if($daytrip_image=='1')
		{
			$max_file_size=$MAX_FILE_SIZE/1024;
			$error_daytrip_image.="Ảnh quá lớn";
			$err="yes";
		}
		else if($daytrip_image=='2')
		{
			$daytrip_image="";
		}	
		
		$daytrip_image_tmp=$daytrip_image;
	}
	else
	{
		if($_FILES['daytrip_image']['name'])
		{
			$myfile=str_replace(" ","_",$_FILES['daytrip_image']['name']);
			$daytrip_image_tmp=time()."_".$myfile;
			$no_origin=1;
		}
	}
	if($err=="" && $daytrip_image_tmp!="")
	{
		//Create thumbnail
		if($IS_CREATE_THUMB){
			for($i=0;$i<count($MAX_THUMB_WIDTH);$i++)
			{
				$MAX_QUALITY[$i]=$MAX_THUMB_WIDTH[$i]>$MAX_THUMB_HEIGHT[$i]?$MAX_THUMB_WIDTH[$i]:$MAX_THUMB_HEIGHT[$i];
				thumbnail_images($_FILES['daytrip_image'], $MAX_THUMB_WIDTH[$i], $MAX_THUMB_HEIGHT[$i], $path, $MAX_QUALITY[$i], "thumb_".$MAX_THUMB_WIDTH[$i], $daytrip_image_tmp, 0, $text); 
			}
		}
		//if($DELETE_ORIGIN_IMAGE)
//		{
//			deletefile($path.$daytrip_image_tmp);
//		}
		if(count($MAX_THUMB_WIDTH) && $no_origin==1)
		{
			$daytrip_image=str_replace(" ","_",$daytrip_image_tmp);
		}
	}
	
	$str_error=$error_daytrip_name;
	if($str_error==""){
		if($type=="ADD"){	
			$sql="INSERT INTO ".pre_table."daytrip(country_id, city_id, daytrip_name, daytrip_price, daytrip_brief, daytrip_image, daytrip_content, daytrip_position, daytrip_isdestination, daytrip_isactive, daytrip_metadescription, daytrip_metakeyword, lang) 
					VALUES	('".$country_id."', '".$city_id."', '".$daytrip_name."', '".$daytrip_price."', '".$daytrip_brief."', '".$daytrip_image."', '".$daytrip_content."', '".$daytrip_position."', '".$daytrip_isdestination."', '".$daytrip_isactive."', '".$daytrip_metadescription."', '".$daytrip_metakeyword."', '".$lang."')";
			execSQL($sql);
			//
			$just_inserted_id=mysql_insert_id();			
			for($i=0;$i<count($travelstyle_id);$i++){
				$sql2="INSERT INTO ".pre_table."daytrip_travelstyle (travelstyle_id, daytrip_id) VALUES ('".$travelstyle_id[$i]."','".$just_inserted_id."')";
				$rs2=execSQL($sql2);
			}
		}else{
			//Create thumbnail
			if($IS_CREATE_THUMB && $old_daytrip_image){
				for($i=0;$i<count($MAX_THUMB_WIDTH);$i++)
				{
					$MAX_QUALITY[$i]=$MAX_THUMB_WIDTH[$i]>$MAX_THUMB_HEIGHT[$i]?$MAX_THUMB_WIDTH[$i]:$MAX_THUMB_HEIGHT[$i];
					thumbnail_images('', $MAX_THUMB_WIDTH[$i], $MAX_THUMB_HEIGHT[$i], $path, $MAX_QUALITY[$i], "thumb_".$MAX_THUMB_WIDTH[$i], $old_daytrip_image, 0, $text); 
				}
			}
			
		    if($old_daytrip_image && $daytrip_image)
			{
				deletefile($path.$old_daytrip_image);
				//Delete thumbnail
				for($i=0;$i<count($MAX_THUMB_WIDTH);$i++)
				{
					deletefile($path."thumb_".$MAX_THUMB_WIDTH[$i]."_".$old_daytrip_image);
				}
			}
			if(!$daytrip_image && $old_daytrip_image)
			{
				$daytrip_image=$old_daytrip_image;
			}
			
			$sql="UPDATE ".pre_table."daytrip SET 
						country_id='".$country_id."', 
						city_id='".$city_id."', 
						daytrip_name='".$daytrip_name."', 
						daytrip_price='".$daytrip_price."', 
						daytrip_brief='".$daytrip_brief."',
						daytrip_image='".$daytrip_image."', 
						daytrip_content='".$daytrip_content."', 
						daytrip_position='".$daytrip_position."', 
						daytrip_isdestination='".$daytrip_isdestination."',
						daytrip_isactive='".$daytrip_isactive."',
						daytrip_metadescription='".$daytrip_metadescription."', 
						daytrip_metakeyword='".$daytrip_metakeyword."', 
						lang='".$lang."'
						WHERE daytrip_id='".$ID."'";			
			execSQL($sql);
			//
			$sql="SELECT daytrip_id FROM ".pre_table."daytrip WHERE daytrip_id='$ID'";
			$rs=execSQL($sql);
			if(mysql_num_rows($rs))
			{
				$old_relative=mysql_result($rs,0,0);
			}
			$sql="SELECT travelstyle_id FROM ".pre_table."daytrip_travelstyle WHERE daytrip_id='$ID'";
			$rs=execSQL($sql);
			$old_relative_arr=array();
			$i=0;
			while ($row=mysql_fetch_array($rs))
			{
				$old_relative_arr[$i]=$row["travelstyle_id"];
				$i++;
			}
			if(count($travelstyle_id)==0)
			{
				$sql_del="DELETE FROM ".pre_table."daytrip_travelstyle WHERE daytrip_id='$ID'";
				$rs_del=execSQL($sql_del);
			}
			else
			{
				for($j=0;$j<count($old_relative_arr);$j++)
				{
					if(!in_array($old_relative_arr[$j],$travelstyle_id))
					{
						$sql_del="DELETE FROM ".pre_table."daytrip_travelstyle WHERE travelstyle_id='".$old_relative_arr[$j]."'  AND daytrip_id = '$ID'";
						$rs_del=execSQL($sql_del);
					}
				}
				for($j=0;$j<count($travelstyle_id);$j++)
				{
					
					if(count($old_relative_arr)==0)
					{
						$sql_ins="INSERT INTO ".pre_table."daytrip_travelstyle(travelstyle_id, daytrip_id) VALUES ('".$travelstyle_id[$j]."','$old_relative')";
						$rs=execSQL($sql_ins);
					}
					else
					{
						if(!in_array($travelstyle_id[$j],$old_relative_arr))
						{
							$sql_ins="INSERT INTO ".pre_table."daytrip_travelstyle(travelstyle_id, daytrip_id) VALUES ('".$travelstyle_id[$j]."','$old_relative')";
							$rs=execSQL($sql_ins);
						}
					}
				}
			}
			//
		}			

		$act = $_POST['act'];
		if($act=='save'){
			echo '<script>
				parent.document.frmData.target = "_parent";				
				parent.window.location.href="daytrip.php";
			</script>';
		}else{	
			echo '<script>
				parent.document.frmData.target = "_parent";	
				parent.document.frmData.daytrip_name.focus();			
				parent.window.location.href="daytripWrite.php";
			</script>';
		}	
		
	}else{
		echo '<script>
				parent.document.frmData.target = "_parent";		
				parent.document.getElementById("err").style.display="block";
				parent.document.getElementById("err_string").innerHTML="'.$str_error.'";
			</script>';	
	}
}
?>